package com.terren.spider.core.html.scheduler;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import org.apache.http.annotation.ThreadSafe;

import us.codecraft.webmagic.Request;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.scheduler.DuplicateRemovedScheduler;
import us.codecraft.webmagic.scheduler.MonitorableScheduler;

@ThreadSafe
public class SpiderQueueScheduler extends DuplicateRemovedScheduler implements MonitorableScheduler {
	
	private BlockingQueue<Request> queue = new LinkedBlockingQueue<Request>();

    @Override
    public void pushWhenNoDuplicate(Request request, Task task) {
        queue.add(request);
    }

    @Override
    public Request poll(Task task) {
        return queue.poll();
    }

    @Override
    public int getLeftRequestsCount(Task task) {
        return queue.size();
    }

    @Override
    public int getTotalRequestsCount(Task task) {
        return getDuplicateRemover().getTotalRequestsCount(task);
    }
	/**
	 * POST的去重：从0.7.1版本开始，POST默认不会去重，详情见：Issue 484。如果想要去重可以自己继承DuplicateRemovedScheduler，重写push方法
	 */
    @Override
    protected boolean noNeedToRemoveDuplicate(Request request) {
    	boolean flag = false;
        return flag;
    }
}
